package com.huarun.common;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.huarun.common.util.HttpServletUtils;


/**
 * 
 * @author hqy
 * @创建时间   2018-10-17 13:54
 * @Description 控制器基类
 */
public class BaseController {
	protected Logger log = LoggerFactory.getLogger(getClass());

	public ResponseMessage<?> getOperResultMap() {
		ResponseMessage<?> responseMessage = ResponseMessage.succMessage("操作成功", null, null);
		return responseMessage;
	}

	public ResponseMessage<?> getOperResultMap(String msg) {
		ResponseMessage<?> responseMessage = ResponseMessage.succMessage(msg, null, null);
		return responseMessage;
	}

	public ResponseMessage<?> getOperResultMap(String msg, Object data) {
		ResponseMessage<?> responseMessage = ResponseMessage.succMessage(msg, data, null);
		return responseMessage;
	}

	public ResponseMessage<?> getOperResultMap(int code, String msg) {
		ResponseMessage<?> responseMessage = ResponseMessage.failureMessage(code, msg, null);
		return responseMessage;
	}
	protected void removeSessionAttribute(String key) {
		HttpServletUtils.getSession().removeAttribute(key);	
	}

	protected Object getSessionAttribute(String key) {
		SessionValueObject sessionObject = (SessionValueObject) HttpServletUtils.getSession().getAttribute(key);
		if (sessionObject == null)
			return null;
		long currTimes = new Date().getTime();
		if (currTimes - sessionObject.getTimes() > 0) {
			HttpServletUtils.getSession().removeAttribute(key);
			return null;
		}
		return sessionObject.getValue();
	}
	
	/**
	 * @author angus
	 * @param key
	 * @param times 有效时长，单位秒 如120 表示2分钟 ,负数或0默认时长
	 * 
	 */
	protected void setSessionAttribute(String key, Object value, long times) {
		SessionValueObject sessionObject = new SessionValueObject();
		sessionObject.setValue(value);
		long currTimes = new Date().getTime();
		if (times <= 0) {
			times = HttpServletUtils.getSession().getMaxInactiveInterval();
		}
		sessionObject.setTimes(currTimes + times * 1000);
		HttpServletUtils.getSession().setAttribute(key, sessionObject);
	}
	
	/**
	 * @author dzm
	 * 
	 */
	private class SessionValueObject {
		private Object value;
		private long times;

		public Object getValue() {
			return value;
		}

		public void setValue(Object value) {
			this.value = value;
		}

		public long getTimes() {
			return times;
		}

		public void setTimes(long times) {
			this.times = times;
		}

	}
}
